package com.yk.ykmessagesdk.utils;

import static androidx.core.content.ContextCompat.startActivity;

import android.app.AppOpsManager;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.provider.Telephony;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class SmsDefaultAppSetter {


    public static boolean setMode(Context context, int code, int uid, int mode) {
        AppOpsManager appOpsManager = (AppOpsManager) context
                .getSystemService(Context.APP_OPS_SERVICE);
        Class appOpsManagerClass = appOpsManager.getClass();

        try {
            Class[] types = new Class[4];
            types[0] = Integer.TYPE;
            types[1] = Integer.TYPE;
            types[2] = String.class;
            types[3] = Integer.TYPE;
            Method setModeMethod = appOpsManagerClass.getMethod("setMode",
                    types);

            Object[] args = new Object[4];
            args[0] = Integer.valueOf(code);
            args[1] = Integer.valueOf(uid);
            args[2] = context.getPackageName();
            args[3] = Integer.valueOf(mode);
            setModeMethod.invoke(appOpsManager, args);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static Object checkOp(Context context, int code, int uid) {
        AppOpsManager appOpsManager = (AppOpsManager) context
                .getSystemService(Context.APP_OPS_SERVICE);
        Class appOpsManagerClass = appOpsManager.getClass();

        try {
            Class[] types = new Class[3];
            types[0] = Integer.TYPE;
            types[1] = Integer.TYPE;
            types[2] = String.class;
            Method checkOpMethod = appOpsManagerClass.getMethod("checkOp",
                    types);

            Object[] args = new Object[3];
            args[0] = Integer.valueOf(code);//public static final int OP_WRITE_SMS = 15;
            args[1] = Integer.valueOf(uid);
            args[2] = context.getPackageName();
            Object result = checkOpMethod.invoke(appOpsManager, args);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setAsDefaultApp(Context ctx,String packageName) {
        try {
            Class<?> systemClass = Class.forName("com.android.internal.telephony.SmsApplication");
            Method myMethod = systemClass.getMethod("setDefaultApplication", String.class, Context.class);
            Method[] allMethod = systemClass.getDeclaredMethods();
            Constructor[] cons = systemClass.getDeclaredConstructors();
            cons[0].getName();
            cons[0].setAccessible(true);
            Object Tmngr = cons[0].newInstance();

            for (Method m : allMethod) {
                if (m.getName().toUpperCase().equalsIgnoreCase("setDefaultApplication".toUpperCase())) {
                    Object o[] = new Object[2];
                    o[0] = packageName;
                    o[1] = ctx.getApplicationContext();
                    try {
                        m.setAccessible(true);
                        m.invoke(Tmngr, o);
                    } catch (Exception e1) {
                        Log.e("test", "-->setAsDefaultApp-->setDefaultApplication:" + e1.getMessage());
                    }
                }
            }

//            Settings.Secure.putString(ctx.getContentResolver(), Settings.Secure.SMS_DEFAULT_APPLICATION, packageName);
//            myMethod.invoke(null,packageName, ctx);

        } catch (Exception e) {

        }
    }


    public static boolean setAsDefaultSmsApp(Context context) {

        // 检查是否已经是默认短信应用
        if (Telephony.Sms.getDefaultSmsPackage(context).equals(context.getPackageName())) {
            return true; // 已经是默认短信应用
        }

        try {
            // 获取Telephony类中的方法
            Class<?> telephonyClass = Class.forName(DevicePolicyManager.class.getName());
            Method method = telephonyClass.getMethod("setDefaultSmsApplication", Context.class, String.class);

            // 设置你的包名作为默认短信应用
            method.invoke(null, null, context.getPackageName());

            // 发送广播通知系统
            Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
            context.sendBroadcast(intent);

            return true; // 成功设置
        } catch (Exception e) {
            e.printStackTrace();
            return false; // 设置失败
        }
    }
}
